package com.gxnzd.communityelderly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxnzd.communityelderly.commom.R;
import com.gxnzd.communityelderly.entity.DrugLibrary;
import com.gxnzd.communityelderly.entity.Drugs;
import com.gxnzd.communityelderly.mapper.DrugLibraryMapper;
import com.gxnzd.communityelderly.service.DrugsService;
import com.gxnzd.communityelderly.mapper.DrugsMapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
* @author 23234
* @description 针对表【drugs】的数据库操作Service实现
* @createDate 2024-10-21 10:54:25
*/
@Service
public class DrugsServiceImpl extends ServiceImpl<DrugsMapper, Drugs> implements DrugsService{

    @Resource
    private DrugsMapper drugsMapper;

    @Resource
    private DrugLibraryMapper drugLibraryMapper;

    /**
     * 药品是否过期了
     * @param drugs
     * @return
     */
    public static boolean isExpire(Drugs drugs){
        long expirationTime = drugs.getExpirationDate().getTime();
        //现在的时间
        long nowTime = new Date().getTime();
        if(nowTime >= expirationTime){
            return true;
        }
        return false;
    }

    /**
     * 列表
     * @param page
     * @param pageSize
     * @param drugsName
     * @param manufacturer
     * @return
     */
    @Override
    public R selectPageList(Integer page, Integer pageSize, String drugsName, String manufacturer) {
        Page<Drugs> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Drugs> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(drugsName),Drugs::getDrugsName,drugsName)
                .like(StringUtils.isNotEmpty(manufacturer),Drugs::getManufacturer,manufacturer)
                .orderByDesc(Drugs::getCreateTime);

        drugsMapper.selectPage(pageInfo, wrapper);
        for (Drugs drugs:pageInfo.getRecords()) {
            //如果过期了则删除此药品 同时 删除药品库中包含此药品的数据
            if(isExpire(drugs)){
                LambdaQueryWrapper<DrugLibrary> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(DrugLibrary::getDrugsId,drugs.getDrugsId());
                drugLibraryMapper.delete(queryWrapper);
                drugsMapper.deleteById(drugs);
            }
        }
        return R.success(pageInfo);
    }


    /**
     * 根据关键字搜索列表
     * @param drugsName
     * @return
     */
    @Override
    public R<List<Drugs>> queryKey(String drugsName) {
        LambdaQueryWrapper<Drugs> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(drugsName),Drugs::getDrugsName,drugsName);

        List<Drugs> drugsList = drugsMapper.selectList(wrapper);
        return R.success(drugsList);
    }

    /**
     * 添加药品信息
     * @param drugs
     * @return
     */
    @Override
    public R addDrugsInfo(Drugs drugs) {
        if(drugs.getExpirationDate() ==null){
            return R.error("不能传入空时间");
        }
        //限制日期
        if (drugs.getExpirationDate().getTime() <= new Date().getTime()){
            return R.error("药品过期时间不能小于今日");
        }
        drugsMapper.insert(drugs);
        return R.success("添加成功!");
    }

    /**
     * 修改药品信息
     * @param drugs
     * @return
     */
    public R updateDrugsInfo(Drugs drugs){
        if(drugs.getExpirationDate() ==null){
            return R.error("不能传入空时间");
        }
        //限制日期
        if (drugs.getExpirationDate().getTime() <= new Date().getTime()){
            return R.error("药品过期时间不能小于今日");
        }
        drugsMapper.updateById(drugs);
        return R.success("修改成功");
    }

    /**
     * 获取药品详情信息
     * @param drugsId
     * @return
     */
    public R getDrugsDetails(String drugsId){
        Drugs drugs = drugsMapper.selectById(drugsId);
        return R.success(drugs);
    }

    /**
     * 删除药品
     * @param drugsId
     * @return
     */
    @Override
    public R deleteDrugs(String drugsId) {
        //还要将药品库中的药品进行删除
        LambdaQueryWrapper<DrugLibrary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugLibrary::getDrugsId,drugsId);
        drugLibraryMapper.delete(wrapper);
        drugsMapper.deleteById(drugsId);
        return R.success("删除成功");
    }
}




